function newPath = RevertNormalisation(initPath, snPath)
% Proceed to the invert normalisation that is registerd in snPath
% The path to the corrected image is given as an output
% The image is put in the coordinates space of the initial PET, which is
% supposedly the initPath without 'sw' at the beginning
[pathstr, name, ext] = fileparts(snPath);
PETPath = fullfile(pathstr, [name(1:end-3) '.nii']);
imagePath = {initPath};
job.comp = {};
job.ofname = '';
job.fnames = imagePath;
job.savedir.savesrc = 1; % save new files in the same directory 
job.interp = 1;
inv.comp={};
inv.space = {PETPath};
sn2def.matname = {snPath};
sn2def.vox = NaN(1,3);
sn2def.bb = NaN(2,3);
inv.comp{1,1} = struct('sn2def',sn2def);
job.comp{1,1}.inv = inv;
% Proceed to the deformation
newPath =  spm_defs(job);
newPath = newPath.warped{1};


% function SPM = RevertNormalisation(SPM, snPath)
% % This function fetches every images created during the statistical analysis
% % and reverts the normalisation step to keep images in the patient space
% 
% %==========================================================================
% % file names created by the Ancova algorithm : Vbeta1 to 4, ResMS, mask
% % and RPV.
% imagePath = {fullfile(SPM.swd,SPM.Vbeta(1).fname);...
%              fullfile(SPM.swd,SPM.Vbeta(2).fname);...
%              fullfile(SPM.swd,SPM.Vbeta(3).fname);...
%              fullfile(SPM.swd,SPM.Vbeta(4).fname);...
%              fullfile(SPM.swd,SPM.VResMS.fname);...
%              fullfile(SPM.swd,SPM.VM.fname);};
% job.comp = {};
% job.ofname = '';
% job.fnames = imagePath;
% job.savedir.savesrc = 1; % save new files in the same directory 
% job.interp = 1;
% inv.comp={};
% swPETPath = SPM.xY.P{1}; %swPETPAth
% [pathstr, name, ext, versn] = fileparts(swPETPath);
% inv.space = {fullfile(pathstr,[name(3:end) ext])};
% sn2def.matname = {snPath};
% sn2def.vox = NaN(1,3);
% sn2def.bb = NaN(2,3);
% inv.comp{1,1} = struct('sn2def',sn2def);
% job.comp{1,1}.inv = inv;
% % Proceed to the deformation
% destFiles = spm_defs(job);
% destFiles = destFiles.warped;
% 
% % Register new results in the SPM structure
% SPM=rmfield(SPM,'Vbeta')
% for i=1:4
%     SPM.Vbeta(i)=spm_vol(destFiles{i});
% end
% SPM.VResMS = spm_vol(destFiles{5});
% SPM.VM = spm_vol(destFiles{6});
% SPM = rmfield(SPM,'xVol');
% SPM.xVol = struct('DIM',SPM.VM.dim,'M',SPM.VM.mat);
             
